﻿using Microsoft.Win32;
using Models.Mailbox;
using Models.Mailbox.Mail;
using Newtonsoft.Json;
using SendMessagePage.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SendMessagePage.HomePage
{
    /// <summary>
    /// TaskConfig.xaml 的交互逻辑
    /// </summary>
    public partial class TaskConfig : Window
    {
        private ObservableCollection<TaskClass> taskList = new ObservableCollection<TaskClass>();

        //查询定时任务
        private string SelectMailboxConfigure = ConfigurationManager.AppSettings["SelectMailboxConfigure"];

        //删除定时任务
        private string DeleteMailboxConfigure = ConfigurationManager.AppSettings["DeleteMailboxConfigure"];

        // private DataGridWindowViewModel _viewModel = null;


        public TaskConfig()
        {
            InitializeComponent();
            Closing += Window_Closing;
            dgvOrderList.ItemsSource = taskList;
  
        }


        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MainWindow mainWindow = new MainWindow();
            if (MessageBox.Show("是否要关闭？", "确认", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                e.Cancel = false;
                mainWindow.Show();


            }
            else
            {
                e.Cancel = true;
            }



        }


        private void AddTask_Click(object sender, RoutedEventArgs e)
        {
            AddTaskPage addTask = new AddTaskPage();
            addTask.ShowDialog();
            

        }

        private async void SelectTask_Click(object sender, RoutedEventArgs e)
        {
            string name =this.txtName.Text;

            if (!string.IsNullOrEmpty(name)) {
                SelectMailboxConfigure += "TaskName=" + name;
            }



            string result = await AccessWebApiUtil.PostAsyncJson(SelectMailboxConfigure, "", PackingUserLogin.token);

            OrderResult<MailboxTaskConfig> orderResult =  JsonConvert.DeserializeObject<OrderResult<MailboxTaskConfig>>(result);

             taskList.Clear();
            if (orderResult.result!=null && orderResult.result.Count>0) {

                foreach (var item in orderResult.result)
                {
                    TaskClass task = new TaskClass() { 
                    Id = item.Id,
                    TaskName  = item.TaskName,
                    TaskRemark = item.TaskRemark,
                    TaskState = item.TaskState==0?"开启":"关闭",
                    TaskTime = item.TaskTime.ToString("yyyy-MM-dd")

                    
                    };


                    taskList.Add(task);
                }
            }
            dgvOrderList.ItemsSource = taskList;

        }


        public void fileClick(Object setPaths)
        {

         MessageBox.Show(string.Empty);

        }

        private async void Delete_Click(object sender, RoutedEventArgs e)
        {

            var SelectedRow = (TaskClass)dgvOrderList.SelectedItem;

            DeleteMailboxConfigure += SelectedRow.Id;

            string result = await AccessWebApiUtil.PostAsyncJson(DeleteMailboxConfigure, "", PackingUserLogin.token);

            OrderResult order = JsonConvert.DeserializeObject<OrderResult>(result);

            if (order != null && order.code == 200)
            {
                MessageBox.Show("删除成功!");
            }
            else {

                MessageBox.Show("删除失败！");
            }

            SelectTask_Click(sender,e);


        }


        private async void Update_Click(object sender, RoutedEventArgs e)
        {
            var SelectedRow = (TaskClass)dgvOrderList.SelectedItem;
            UpdateTaskPage updateTask = new UpdateTaskPage(SelectedRow);
            updateTask.ShowDialog();




            SelectTask_Click(sender, e);


        }


    }
}
